package cn.qidisoft.edu.hzjt.service.xb.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.edu.hzjt.dao.xb.PartyIncorruptAttMapper;
import cn.qidisoft.edu.hzjt.dao.xb.PartyIncorruptMapper;
import cn.qidisoft.edu.hzjt.model.xb.PartyIncorruptAttEn;
import cn.qidisoft.edu.hzjt.model.xb.PartyIncorruptEn;
import cn.qidisoft.edu.hzjt.model.xb.PartyIncorruptRangerEn;
import cn.qidisoft.edu.hzjt.service.xb.IPartyIncorruptService;
import cn.qidisoft.edu.hzjt.utils.LogType;

@Service
public class PartyIncorruptServiceImpl implements IPartyIncorruptService {

	protected static final LoggerAdapter LOGGER = LoggerAdapterFactory
			.getLogger(LogType.HZJT_LOGGER.val);
	
	@Autowired
	private PartyIncorruptMapper mapper;
	
	@Autowired
	private PartyIncorruptAttMapper mapperAtt;
	
	@Override
	public List<PartyIncorruptEn> queryList(PartyIncorruptEn model) {
		return mapper.queryList(model);
	}

	@Override
	public PaginationBean<PartyIncorruptEn> queryPageList(PartyIncorruptEn model,
			Page page) {
		try {
			int totalRecords = mapper.queryPageListcount(model);
			
			 PaginationBean<PartyIncorruptEn> pageHolder = new PaginationBean<>(page, totalRecords);
	            List<PartyIncorruptEn> resultList = mapper
	                    .queryPageList(model,
	                            new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
	                                    pageHolder.getPageSize()));
	            pageHolder.setPageList(resultList);
	            return pageHolder;
		} catch (Exception e) {
			LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                    this.getClass().getName(),
                    ToStringBuilder.reflectionToString(model, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                    e);
		}
	}

	@Override
	public PartyIncorruptEn queryByPK(PartyIncorruptEn model) {
		return mapper.queryByPK(model);
	}

	@Override
	public int insert(PartyIncorruptEn model) {
		return mapper.insert(model);
	}

	@Override
	public int update(PartyIncorruptEn model) {
		mapper.update(model);
		return 0;
	}

	@Override
	public int delete(PartyIncorruptEn model) {
		mapper.delete(model);
		return 0;
	}

	@Override
	public List<Map<String, Object>> queryAllYearList() {
		return mapper.queryAllYearList();
	}

	@Override
	public PaginationBean<PartyIncorruptEn> queryPageList2(PartyIncorruptEn PartyIncorruptEn,
			Page page) {
		try {
			int totalRecords = mapper.queryPageListcount2(PartyIncorruptEn);
			
			 PaginationBean<PartyIncorruptEn> pageHolder = new PaginationBean<>(page, totalRecords);
	            List<PartyIncorruptEn> resultList = mapper
	                    .queryPageList2(PartyIncorruptEn,
	                            new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
	                                    pageHolder.getPageSize()));


	            List<PartyIncorruptEn> lst = new ArrayList<>();
				PartyIncorruptAttEn att;
				for (PartyIncorruptEn PartyIncorruptEn2 : resultList) {
					PartyIncorruptEn en = new PartyIncorruptEn();
					ConvertUtils.register(new DateConverter(null), java.util.Date.class);

					BeanUtils.copyProperties(en, PartyIncorruptEn2);
					att = new PartyIncorruptAttEn();
					att.setPyear(PartyIncorruptEn2.getlzNf());
					List<PartyIncorruptAttEn> attList = mapperAtt.queryList2(att);
					en.setlzNfYear(PartyIncorruptEn2.getlzNf()+"年");
					en.setAttList(attList);

					lst.add(en);
				}

				pageHolder.setPageList(lst);
				return pageHolder;
		} catch (Exception e) {
			LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                    this.getClass().getName(),
                    ToStringBuilder.reflectionToString(PartyIncorruptEn, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                    e);
		}
	}

	@Override
	public List<PartyIncorruptEn> exportList(PartyIncorruptEn model, Page page) {
		try {
			PaginationBean<PartyIncorruptEn> pageHolder = new PaginationBean<>(page, 999999);
            List<PartyIncorruptEn> resultList = mapper
                    .queryPageList2(model,
                            new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                                    pageHolder.getPageSize()));


            List<PartyIncorruptEn> lst = new ArrayList<>();
			PartyIncorruptAttEn att;
			for (PartyIncorruptEn PartyIncorruptEn2 : resultList) {
				PartyIncorruptEn en = new PartyIncorruptEn();
				ConvertUtils.register(new DateConverter(null), java.util.Date.class);

				BeanUtils.copyProperties(en, PartyIncorruptEn2);
				att = new PartyIncorruptAttEn();
				att.setPyear(PartyIncorruptEn2.getlzNf());
				List<PartyIncorruptAttEn> attList = mapperAtt.queryList2(att);
				en.setlzNfYear(PartyIncorruptEn2.getlzNf()+"年");
				en.setAttList(attList);

				lst.add(en);
			}
			
			return lst;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<PartyIncorruptRangerEn> plist(int parentId) {
		return mapper.plist(parentId);
	}

	@Override
	public void batchInserts(List<PartyIncorruptRangerEn> records) {
		mapper.batchInserts(records);
		
	}

	@Override
	public int deleteByParentId(int parentId) {
		try {
			mapper.deleteByParentId(parentId);
		} catch (Exception e) {
			e.printStackTrace();
			
			return -1;
		}
		return 0;
	}

}
